import common from './common'

const baseUrl = "https://x.bilinmeiju.com/app/"



function request(url, params = {}, callback) {
	var methods = 'POST';
	return new Promise((resolve, reject) => {
		const URL = baseUrl + url
		const DATA = Object.assign({}, params)
		switch (methods) {
			case "GET":
				uni.request({
					url: URL,
					method: 'GET',
					header: {
						'token': uni.getStorageSync("token"),
						'content-type': 'application/json'
					},
					data: DATA,
					success(res) {
						if (res.data.code === 200) {
							callback(res)
						} else if (res.data.code !== 200) {
							common.toast("未知错误，请重试")
						}
					},
					fail(err) {
						common.toast("网络错误，请重试")
						reject(err)
					}
				})
				break;
			case "POST":
				uni.request({
					url: URL,
					method: 'POST',
					header: {
						'token': uni.getStorageSync("token"),
						'content-type': 'application/json'
					},
					data: DATA,
					success(res) {
						if (res.data.code === 0) {
							callback(res)
						} else if (res.data.code == 10086) {
							uni.setStorageSync('token', '');
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
						}
					}
				})
				break;
		}
	})

}

export default {
	request,
	baseUrl
}
